package rollercoaster.railway;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.media.opengl.GL2ES2;

import rollercoaster.commons.Material;
import rollercoaster.commons.Model;
import rollercoaster.commons.Node;
import rollercoaster.commons.Vertex3D;
import rollercoaster.commons.interfaces.IVertexReader;
import rollercoaster.commons.interfaces.NodoProducer;
import rollercoaster.commons.interfaces.SurfaceTessellationAlgorithm;
import rollercoaster.environment.interfaces.IModelReader;
import rollercoaster.environment.utils.ModelMaker;
import rollercoaster.railway.interfaces.BinarioDataReader;
import rollercoaster.railway.interfaces.IPolePosition;
import rollercoaster.railway.utils.BinarioParameterContainer;
import rollercoaster.railway.utils.PolePosition;

public class MagneticRailProducer extends RailProducer{

	public static final float module=0.8f;
	
	
	
	public MagneticRailProducer(SurfaceTessellationAlgorithm tess,
			BinarioDataReader binarioReader, IVertexReader vertexReader) {
		super(tess, binarioReader, vertexReader);
		// TODO Auto-generated constructor stub
	}

	public Node getNode(GL2ES2 gl) {
		BinarioParameterContainer bpc = initRailParameters(gl);
		
		Float ray=Float.parseFloat(bpc.getMiddlemap().get("tube-ray"));
		Tube middlesurface=new Tube(bpc.getTr(), new RoundedRectangle(2*ray, 2*ray, ray/4), new Vertex3D(0, 0, 0));
		
		float x=module;
		Float raylat=Float.parseFloat(bpc.getLateralmap().get("tube-ray"));
		Tube sxsurface=new Tube(bpc.getTr(), new RoundedRectangle(2*raylat, 2*raylat, raylat/4), new Vertex3D(-x, ray/2, 0));
		Tube dxsurface=new Tube(bpc.getTr(), new RoundedRectangle(2*raylat, 2*raylat, raylat/4), new Vertex3D(x, ray/2, 0));
		distanceBetweenRails=new Vertex3D(x, ray/2, 0);
		
		Float raylittle=Float.parseFloat(bpc.getLittlemap().get("tube-ray"));
		Tube fascia=new Tube(bpc.getTr(), new RoundedRectangle(2*x, raylittle*2, raylittle/4), new Vertex3D(0, ray/2, 0));

		PolePosition positioner=new PolePosition(binarioReader.getSupportPositionInfo().get("support-position"));
		List<Float> positions=positioner.getPolePositions();
		List<Tube> tubi=new ArrayList<Tube>();
		Float rayS=Float.parseFloat(bpc.getSupportmap().get("tube-ray"));
		float h_base=-1.0f;
		for (Float float1 : positions) {
			Vertex3D top_pos=middlesurface.getTrajectory().getBase().gamma(float1);
			Segment seg=new Segment(new Vertex3D(top_pos.getX(), top_pos.getY(),h_base), top_pos);
			Segment seg_up=new Segment(new Vertex3D(top_pos.getX()+0.1f, top_pos.getY(), h_base), 
								new Vertex3D(top_pos.getX()+0.1f, top_pos.getY(), top_pos.getZ()));
			Trajectory traj=new Trajectory(seg, seg_up);
			Tube palone=new Tube(traj, new RoundedRectangle(rayS*2,rayS*2,rayS/16), new Vertex3D(0, 0, 0));
			tubi.add(palone);
		}
		
		List<Model> modellini=new ArrayList<Model>();

		Model modelMiddle = produceModel(gl,bpc.getMaterialmap(), bpc.getMiddlemap(),middlesurface);
		modellini.add(modelMiddle);
		
		Model modelSx=produceModel(gl,bpc.getMaterialmap(), bpc.getLateralmap(), sxsurface);
		modellini.add(modelSx);
		
		Model modelDx=produceModel(gl,bpc.getMaterialmap(), bpc.getLateralmap(), dxsurface);
		modellini.add(modelDx);
		
		Model modelFasciaSx=produceModel(gl, bpc.getMaterialmap(), bpc.getLittlemap(), fascia);
		modellini.add(modelFasciaSx);
	
		for (Tube tubo : tubi) {
			Model paloModel=produceModel(gl, bpc.getMaterialmap(), bpc.getSupportmap(), tubo);
			modellini.add(paloModel);
		}
		
		return new Node(modellini);
	}
	
	
}
